/**
 * COPYRIGHT HangZhou Trustway Technology Company Limited
 * All right reserved.
 */
package com.robam.cloud.authcenter.service.impl;

import com.robam.cloud.authcenter.dao.AuthPlatformMapper;
import com.robam.cloud.authcenter.helper.WechatHelper;
import com.robam.cloud.authcenter.service.WechatService;
import com.robam.cloud.common.bean.authcenter.entity.AuthPlatform;
import com.robam.cloud.common.bean.authcenter.entity.WechatSignParam;
import com.robam.cloud.common.bean.authcenter.enumspace.AuthPlatformTypeEnum;
import com.robam.cloud.common.bean.restful.entity.ServiceException;
import com.robam.cloud.common.bean.usercenter.entity.LoginAccount;
import com.robam.cloud.restful.config.initdata.CurrentAccountAndUserData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description
 * @Author 薛铁琪
 * @CreateTime 2020-08-04 9:01
 * @Version 1.0
 */
@Service
public class WechatServiceImpl implements WechatService {
    @Autowired
    AuthPlatformMapper authPlatformMapper;
    @Autowired
    CurrentAccountAndUserData currentAccountAndUserData;
    @Autowired
    WechatHelper wechatHelper;

    @Override
    public WechatSignParam getSignParams(String url, HttpServletRequest httpServletRequest) {
        LoginAccount loginAccount = currentAccountAndUserData.getCurrentLoginAccount(httpServletRequest);
        AuthPlatform authPlatform = this.authPlatformMapper.selectByPrimaryKey(loginAccount.getPlatformId());
        if (!AuthPlatformTypeEnum.WECHAT.getCode().equals(authPlatform.getPaltformType())) {
            throw new ServiceException("非微信平台无法获取微信jsapi参数请检查平台配置");
        }
        return wechatHelper.getSignPramByAuthPlat(authPlatform, url);
    }

    @Override
    public WechatSignParam getSignParamsFace(String url, HttpServletRequest httpServletRequest) {
        LoginAccount loginAccount = currentAccountAndUserData.getCurrentLoginAccount(httpServletRequest);
        AuthPlatform authPlatform = this.authPlatformMapper.selectByPrimaryKey(loginAccount.getPlatformId());
        AuthPlatform faceAuthplatform = this.authPlatformMapper.selectByUuid(authPlatform.getFaceUuid());
        if (!AuthPlatformTypeEnum.WECHAT.getCode().equals(faceAuthplatform.getPaltformType())) {
            throw new ServiceException("扫脸模式非微信平台无法获取微信jsapi参数请检查平台配置");
        }
        return wechatHelper.getSignPramByAuthPlat(faceAuthplatform, url);
    }
}
